package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.commonutils.RandomUtil;
import com.commonutils.results;
import com.entity.pojo.Resources;
import com.entity.vo.ResourceVo;
import com.service.ResourcesService;
import com.utils.InitData;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chenMo
 * @since 2022-04-21
 */
@RestController
@RequestMapping("/resources")
@CrossOrigin
public class ResourcesController {
    @Resource
    private ResourcesService resourcesService;

    @Resource
    private InitData initData;

    @ApiOperation("组合条件查询资源信息")
    @PostMapping("/pageResourcesCondition/{current}/{limit}")
    public results pageResourcesCondition(@PathVariable long current,
                                          @PathVariable long limit,
                                          @RequestBody(required = false) ResourceVo resourceQuery) {
//        resourceQuery.setIsDeleted(0);
        List<ResourceVo> resourceList = resourcesService.pageResourcesCondition(current, limit, resourceQuery);
        int total = resourceList.size();
        return results.ok().data("resourceList", resourceList).data("total", total);
    }

    @ApiOperation("增加新的资源")
    @PostMapping("addResource")
    public results addResource(@RequestBody Resources resource) {
        if (!StringUtils.hasText(resource.getResourceContent())) {
            return results.error("添加失败，目录名称不能为空");
        }
        initData.initResource(resource);
        boolean saveFlag = resourcesService.save(resource);
        if (saveFlag) {
            return results.ok();
        } else {
            return results.error("未知错误，添加新资源失败");
        }
    }

    // 初始化资源信息
    private void initInfo(Resources resource) {
        // 设置排序字段
        List<Resources> list = resourcesService.list(new QueryWrapper<Resources>().eq("conceptId", resource.getConceptId()).select("positionId"));
        if (list == null || list.size() == 0) {
            resource.setPositionId(1);
        } else {
            int maxPositionId = list.stream().mapToInt(Resources::getPositionId).summaryStatistics().getMax();
            resource.setPositionId(maxPositionId + 1);
        }
        // 设置唯一性代码
        Integer maxResourceId;
        Resources resources = resourcesService.list(new QueryWrapper<Resources>().select("max(resourceId)")).get(0);
        if (resources == null) {
            maxResourceId = 1;
        }else {
            maxResourceId = resources.getResourceId();
        }
        String code = "D" + RandomUtil.getSixBitRandom() + maxResourceId;
        resource.setResourceCode(code);
    }

    @ApiOperation("修改课程资源")
    @PutMapping("updateResource")
    public results updateResource(@RequestBody Resources resources) {
        boolean updateFlag = resourcesService.updateById(resources);
        if (updateFlag) {
            return results.ok();
        } else {
            return results.error("修改资源信息失败");
        }
    }

    @ApiOperation("删除资源")
    @DeleteMapping("deleteResource/{recourseId}")
    public results deleteResource(@PathVariable Integer recourseId) {
        boolean removeFlag = resourcesService.removeById(recourseId);
        if (removeFlag) {
            return results.ok();
        } else {
            return results.error("删除资源失败");
        }
    }
}

